"""
_*_ : utf-8 _*_ 
@Time: 2023/10/31 22:00
@Author: LWK
@File: 061_ajax_豆瓣电影
@Project: demo01
"""
import json
import urllib.request
import urllib.parse

url = 'https://movie.douban.com/j/chart/top_list?'

headers = {
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) '
                  'Chrome/118.0.0.0 Safari/537.36 Edg/118.0.2088.76'
}

params = {
    'type': '10',
    'interval_id': '100:90',
    'action': '',
    'start': '0',
    'limit': '20'
}

data = urllib.parse.urlencode(params)

print(data)

req = urllib.request.Request(url + data, None, headers)

res = urllib.request.urlopen(req)

content = res.read().decode('utf-8')

# open方法默认情况下使用的是GBK的编码 如果我们想板寸汉字，那么需要再open方法中指定编码格式为utf-8
# fp = open('../temp/douban.json', 'w', encoding='utf-8')
# fp.write(content)
# fp.close()
with open('../temp/douban.json', 'w', encoding='utf-8') as fp:
    fp.write(content)




